/* SQUIB - Scala's Quirky User Interface Builder 
    Copyright (C) 2008 Tim Dalton

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

import scala.collection.mutable.{HashMap}
    
package tfd.scala.squib {
   
    import java.awt.Component
    import javax.swing.event.{InternalFrameEvent, InternalFrameListener}
    import java.util.EventObject

    trait FiresInternalFrameEvents[T <: {  def addInternalFrameListener(ifl:InternalFrameListener) }] { self:FiresEventObjects with IdableTrait =>
        self.eventHandlers += (internalFrameEvents _).asInstanceOf[(Object, HashMap[String, EventObject => Unit]) => Unit]

        def internalFrameEvents(comp: T, events:HashMap[String, EventObject => Unit]):T = {
            val handler = new DelegateinternalFrameEventHandler(events);
            if (handler.hasEvents) {
                comp.addInternalFrameListener(handler)
            }
            comp
        }

        class DelegateinternalFrameEventHandler(events: HashMap[String, EventObject => Unit]) extends InternalFrameListener {
            private val internalFrameActivatedDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframeactivated");
            private val internalFrameClosedDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframeclosed");
            private val internalFrameClosingDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframeclosing");
            private val internalFrameDeactivatedDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframedeactivated");
            private val internalFrameDeiconifiedDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframedeiconified");
            private val internalFrameIconifiedDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframeiconified");
            private val internalFrameOpenedDelegate:Option[InternalFrameEvent => Unit] = events.get("internalframeopened");

            lazy val hasEvents = !(internalFrameActivatedDelegate.isEmpty
                                   && internalFrameClosedDelegate.isEmpty
                                   && internalFrameClosingDelegate.isEmpty
                                   && internalFrameDeactivatedDelegate.isEmpty
                                   && internalFrameDeiconifiedDelegate.isEmpty
                                   && internalFrameIconifiedDelegate.isEmpty
                                   && internalFrameOpenedDelegate.isEmpty)
            
            override def internalFrameActivated(ife: InternalFrameEvent) {
                if (!internalFrameActivatedDelegate.isEmpty) internalFrameActivatedDelegate.get.apply(ife)
            }

            override def internalFrameClosed(ife: InternalFrameEvent) {
                if (!internalFrameClosedDelegate.isEmpty) internalFrameClosedDelegate.get.apply(ife)
            }

            override def internalFrameClosing(ife: InternalFrameEvent) {
                if (!internalFrameClosingDelegate.isEmpty) internalFrameClosingDelegate.get.apply(ife)
            }

            override def internalFrameDeactivated(ife: InternalFrameEvent) {
                if (!internalFrameDeactivatedDelegate.isEmpty) internalFrameDeactivatedDelegate.get.apply(ife)
            }

            override def internalFrameDeiconified(ife: InternalFrameEvent) {
                if (!internalFrameDeiconifiedDelegate.isEmpty) internalFrameDeiconifiedDelegate.get.apply(ife)
            }

            override def internalFrameIconified(ife: InternalFrameEvent) {
                if (!internalFrameIconifiedDelegate.isEmpty) internalFrameIconifiedDelegate.get.apply(ife)
            }

            override def internalFrameOpened(ife: InternalFrameEvent) {
                if (!internalFrameOpenedDelegate.isEmpty) internalFrameOpenedDelegate.get.apply(ife)
            }
        }
    }
}
